function ti = transimg(varargin)
% transimg  - class and constructor for transimg objects
%
% Fields (properties):
%
%  .Width                           width (1x1 double)
%  .Height                          height (1x1 double)
%  .Background                      background RGB (1x3 double)
%  .IsRendered                      boolean flag, needs rendering?
%  .Rendered                        after call to render contains the
%                                   Width x Height x 3 RGB uint8 image
%  .Layer(L).Pixel                  pixel information of layer L
%           .Alpha                  alpha information of layer L
%  .Handle                          associated image handle (or empty)
%
% Methods
%
% TI = transimg(WIDTH, HEIGHT); constructor
% addlayer(TI, PIXELS, ALPHA);  add a layer of pixels/alpha information
% avglayers(TI, AVGINDEX);      average layers (into first of index)
% delete(TI);                   remove object from global storage
% dellayer(TI, DELINDEX);       remove the named layer(s)
% display(TI);                  set handle with CData (create if necessary)
% joinlayers(TI, JOININDEX);    joins the named layers into one layer
% render(TI);                   render the layers into .Rendered
% sethandle(TI, HANDLE);        set graphics handle (of type image)

% Version:  v0.8a
% Build:    9112017
% Date:     Nov-20 2009, 5:01 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% global variables for storage
global tiobj ...
       tiobjlup;

% initialize
if isempty(tiobj)
    tiobj = newtransimg;
    tiobjlup = 0;
end

% if input is of type transimg, special uses
if nargin > 0 && ...
    isa(varargin{1}, 'transimg')
end

% preset width and height to 0
height = 0;
width = 0;

% width and height constructor
if nargin > 1 && ...
    isa(varargin{1}, 'double') && ...
    numel(varargin{1}) == 1 && ...
    isa(varargin{2}, 'double') && ...
    numel(varargin{2}) == 1
    width = varargin{1};
    height = varargin{2};
end
if nargin > 2 && ...
    isa(varargin{3}, 'double') && ...
    numel(varargin{3}) == 3
    bgcol = varargin{3}(:)';
    bgcol(isinf(bgcol) | isnan(bgcol) | bgcol < 0) = 0;
    while any(bgcol > 1)
        bgcol = (1 / 256) .* bgcol;
    end
else
    bgcol = [0, 0, 0];
end
bgcol = uint8(round(255 .* bgcol));
uo = uint8(ones(height, width));

% create new image im global storage
tiobj(end + 1) = newtransimg;
lup = rand(1, 1);
while any(tiobjlup == lup)
    lup = rand(1, 1);
end
tiobjlup(end + 1) = lup;

% make settings
tiobj(end).Width = width;
tiobj(end).Height = height;
tiobj(end).Background = uint8(bgcol);
tiobj(end).Rendered = cat(3, bgcol(1) .* uo, bgcol(2) .* uo, bgcol(3) .* uo);
tiobj(end).IsRendered = true;
tiobj(end).Handle = [];

% then create object
ti = class(struct('L', lup), 'transimg');

% subfunction
function no = newtransimg
no = struct( ...
    'Width'     , 0, ...
    'Height'    , 0, ...
    'Background', [0, 0, 0], ...
    'IsRendered', false, ...
    'Rendered'  , uint8(zeros(0, 0, 3)), ...
    'Layer'     , cell2struct(cell(0, 0, 3), {'Pixel', 'Alpha', 'Ref'}, 3), ...
    'Handle'    , []);

